package cn.yusei.teamTribe.controller.post;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.constant.TextType;
import cn.yusei.teamTribe.entity.Post;
import cn.yusei.teamTribe.exception.PermissionForbiddenException;
import cn.yusei.teamTribe.exception.PostNotFoundException;
import cn.yusei.teamTribe.service.TextFilterService;
import cn.yusei.teamTribe.service.post.PostService;
import cn.yusei.teamTribe.util.ResponseUtil;

@Controller
public class PostController {

	@Autowired
	private PostService postService;
	@Autowired
	private TextFilterService textFilterService;
	
	@RequestMapping(value = "/post/add")
	public void add(@RequestAttribute int id, @RequestParam int teamId, Post post, PrintWriter out) {
		post.setTitle(textFilterService.getFilterString(post.getTitle(), TextType.POST));
		post.setContent(textFilterService.getFilterString(post.getContent(), TextType.POST));
		if (teamId < 0 || !post.isValid()) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int postId = 0;
		try {
			postId = postService.add(id, teamId, post);
		} 
		catch (PermissionForbiddenException e) {
			ResponseUtil.responseCode(out, ResponseCode.PERMISSION_FORBIDDEN);
			return;
		}
		catch(PostNotFoundException e) {
			ResponseUtil.responseCode(out, ResponseCode.POST_NOT_EXISTS);
			return;
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", postId);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}

	@RequestMapping(value = "/post/delete")
	public void delete(@RequestAttribute int id, @RequestParam("id") int postId, PrintWriter out) {
		if (postId <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try {
			code = postService.delete(id, postId);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}

	@RequestMapping(value = "/post/update")
	public void update(@RequestAttribute int id, Post post, PrintWriter out) {
		post.setTitle(textFilterService.getFilterString(post.getTitle(), TextType.POST));
		post.setContent(textFilterService.getFilterString(post.getContent(), TextType.POST));
		if (post.getId() <= 0 || !post.isValid()) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try {
			code = postService.update(id, post);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}

	@RequestMapping(value = "/post/getById")
	public void getById(@RequestParam int id, PrintWriter out) {
		if (id <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		Post post = null;
		try {
			post = postService.get(id);
		} catch (PostNotFoundException e) {
			ResponseUtil.responseCode(out, ResponseCode.POST_NOT_EXISTS);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("post", post);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}

	@RequestMapping(value = "/post/getByUser")
	public void getByUser(@RequestParam int id, @RequestParam int offset, @RequestParam int num, PrintWriter out) {
		if (id <= 0 || offset < 0 || num <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int[] ids = null;
		try {
			ids = postService.getByUserId(id, offset, num);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", ids);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}

	/**
	 * 
	 * @param id
	 *            不能为0
	 * @param offset
	 * @param num
	 * @param out
	 */
	@RequestMapping(value = "/post/getByTargetId")
	public void getByTargetId(@RequestParam int id, @RequestParam int offset, @RequestParam int num, PrintWriter out) {
		if (id <= 0 || offset < 0 || num <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int[] ids = null;
		try {
			ids = postService.getByTargetId(id, offset, num);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", ids);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}

	/**
	 * 
	 * @param id
	 *            社团的 id
	 * @param offset
	 * @param num
	 * @param out
	 */
	@RequestMapping(value = "/post/getByTeam")
	public void getByTeam(@RequestParam int id, @RequestParam int offset, @RequestParam int num, PrintWriter out) {
		if (id < 0 || offset < 0 || num <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int[] ids = null;
		try {
			ids = postService.getByTeam(id, offset, num);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", ids);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
}
